function sieve(n) {
    if (n <= 2) return new Array(n).fill(false);
    const isPrime = new Array(n).fill(true);
    isPrime[0] = isPrime[1] = false;
    const sqrtN = Math.floor(Math.sqrt(n)) + 1;
    
    for (let i = 2; i < sqrtN; i++) {
        if (isPrime[i]) {
            for (let j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }
    }
    return isPrime;
}

function main() {
    const args = process.argv.slice(2);
    const n = args[0] ? parseInt(args[0]) : 10_000_000;
    const runs = args[1] ? parseInt(args[1]) : 5;
    const printFlag = args[2] !== undefined; // 第三个参数存在则打印
    let totalTime = 0;
    
    // 预热运行
    for (let i = 0; i < 3; i++) {
        sieve(1_000_000);
    }
    
    for (let i = 0; i < runs; i++) {
        const start = performance.now();
        const primes = sieve(n);
        const end = performance.now();
        if (printFlag) {
            console.log(primes);
        }
        totalTime += (end - start);
    }
    
    const avgTime = totalTime / runs;
    console.log(`JS     | ${avgTime.toFixed(2)} ms`);
}

main();